package my.dao.daoImpl;

import my.dao.CommonDao;
import my.utils.ObjUtils;
import my.vo.voImpl.BookType;

import java.util.LinkedList;
import java.util.List;

/**
 * @author 星环
 * @date 2022/11/13 13:29:59
 * @description
 */
public class BookTypeDao extends CommonDao<BookType> {

    private static final BookTypeDao bookTypeDao = new BookTypeDao();

    private BookTypeDao(){
        super(new BookType());
    }

    public static BookTypeDao getInstance(){
        return bookTypeDao;
    }

    public BookType deleteById(int id){
        List<BookType> bookTypes = deleteBy(bookType -> bookType.getId() == id);
        return (BookType) ObjUtils.getFirstOrNull(bookTypes);
    }

    public List<BookType> deleteByName(String name){
        return deleteBy(bookType -> bookType.getName().equals(name));
    }

    public List<BookType> updateById(BookType bookType){
        List<BookType> bookTypes = list.stream().filter(bookType1 -> bookType.getId() == bookType1.getId()).toList();
        bookTypes.forEach(bookType1 -> {
            if(bookType1.getName() != null){
                bookType1.setName(bookType.getName());
            }
        });
        return viewOf(bookTypes);
    }

    public List<BookType> getAll(){
        return srchBy(bookType -> true);
    }

    public List<BookType> getByName(String name){
        return srchBy(bookType -> bookType.getName().equals(name));
    }

    public BookType getById(int id){
        List<BookType> bookTypes = srchBy(bookType -> bookType.getId() == id);
        return (BookType) ObjUtils.getFirstOrNull(bookTypes);
    }

    /**
     * 可能有多个一样的类别名 所以返回列表
     * @param name 类别名
     * @return 同名的类别的id列表
     */
    public List<Integer> getIdByName(String name){
        List<BookType> bookTypes = getByName(name);
        List<Integer> resList = new LinkedList<>();
        bookTypes.forEach(bookType -> resList.add(bookType.getId()));
        return resList;
    }

    public String getNameById(int newId) {
        BookType bookType = getById(newId);
        return bookType.getName();
    }
}
